#!/bin/bash

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 项目配置
PROJECT_NAME="weibo-crawler"
IMAGE_NAME="weibo-crawler"
VERSION="latest"

# 打印带颜色的消息
print_info() {
    echo -e "${BLUE}[INFO]${NC} $1"
}

print_success() {
    echo -e "${GREEN}[SUCCESS]${NC} $1"
}

print_warning() {
    echo -e "${YELLOW}[WARNING]${NC} $1"
}

print_error() {
    echo -e "${RED}[ERROR]${NC} $1"
}

# 检查Docker是否安装
check_docker() {
    if ! command -v docker &> /dev/null; then
        print_error "Docker未安装，请先安装Docker"
        exit 1
    fi
    
    if ! docker info &> /dev/null; then
        print_error "Docker未运行，请启动Docker"
        exit 1
    fi
}

# 构建镜像
build_image() {
    print_info "开始构建Docker镜像..."
    
    # 检查是否存在必要文件
    if [ ! -f "Dockerfile" ]; then
        print_error "Dockerfile不存在"
        exit 1
    fi
    
    if [ ! -f "requirements.txt" ]; then
        print_error "requirements.txt不存在"
        exit 1
    fi
    
    # 构建镜像
    docker build -t "${IMAGE_NAME}:${VERSION}" . --no-cache
    
    if [ $? -eq 0 ]; then
        print_success "Docker镜像构建成功"
    else
        print_error "Docker镜像构建失败"
        exit 1
    fi
}

# 运行容器
run_container() {
    print_info "启动容器..."
    
    # 检查config.json是否存在
    if [ ! -f "config.json" ]; then
        print_warning "config.json不存在，请确保配置文件正确"
    fi
    
    # 创建必要的目录
    mkdir -p weibo log
    
    # 运行容器
    docker run -d \
        --name "${PROJECT_NAME}" \
        --restart unless-stopped \
        -v "$(pwd)/config.json:/app/config.json:ro" \
        -v "$(pwd)/weibo:/app/weibo" \
        -v "$(pwd)/log:/app/log" \
        -e TZ=Asia/Shanghai \
        "${IMAGE_NAME}:${VERSION}"
    
    if [ $? -eq 0 ]; then
        print_success "容器启动成功"
        print_info "容器名称: ${PROJECT_NAME}"
        print_info "查看日志: docker logs ${PROJECT_NAME}"
        print_info "停止容器: docker stop ${PROJECT_NAME}"
    else
        print_error "容器启动失败"
        exit 1
    fi
}

# 使用docker-compose运行
run_compose() {
    print_info "使用docker-compose启动服务..."
    
    if [ ! -f "docker-compose.yml" ]; then
        print_error "docker-compose.yml不存在"
        exit 1
    fi
    
    # 创建必要的目录
    mkdir -p weibo log
    
    # 启动服务
    docker-compose up -d
    
    if [ $? -eq 0 ]; then
        print_success "服务启动成功"
        print_info "查看服务状态: docker-compose ps"
        print_info "查看日志: docker-compose logs -f"
        print_info "停止服务: docker-compose down"
    else
        print_error "服务启动失败"
        exit 1
    fi
}

# 停止并删除容器
stop_container() {
    print_info "停止并删除容器..."
    
    if docker ps -a --format "table {{.Names}}" | grep -q "^${PROJECT_NAME}$"; then
        docker stop "${PROJECT_NAME}"
        docker rm "${PROJECT_NAME}"
        print_success "容器已停止并删除"
    else
        print_warning "容器不存在或未运行"
    fi
}

# 清理镜像
clean_image() {
    print_info "清理Docker镜像..."
    
    # 删除项目镜像
    if docker images --format "table {{.Repository}}:{{.Tag}}" | grep -q "^${IMAGE_NAME}:${VERSION}$"; then
        docker rmi "${IMAGE_NAME}:${VERSION}"
        print_success "镜像已删除"
    else
        print_warning "镜像不存在"
    fi
    
    # 清理悬空镜像
    docker image prune -f
    print_success "悬空镜像已清理"
}

# 查看日志
view_logs() {
    if docker ps --format "table {{.Names}}" | grep -q "^${PROJECT_NAME}$"; then
        print_info "查看容器日志..."
        docker logs -f "${PROJECT_NAME}"
    else
        print_error "容器未运行"
        exit 1
    fi
}

# 显示帮助信息
show_help() {
    echo "用法: $0 [选项]"
    echo ""
    echo "选项:"
    echo "  build     构建Docker镜像"
    echo "  run       运行Docker容器"
    echo "  compose   使用docker-compose运行"
    echo "  stop      停止并删除容器"
    echo "  clean     清理Docker镜像"
    echo "  logs      查看容器日志"
    echo "  help      显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 build     # 构建镜像"
    echo "  $0 run       # 运行容器"
    echo "  $0 compose   # 使用docker-compose运行"
    echo "  $0 stop      # 停止容器"
    echo "  $0 clean     # 清理镜像"
    echo "  $0 logs      # 查看日志"
}

# 主函数
main() {
    check_docker
    
    case "$1" in
        build)
            build_image
            ;;
        run)
            build_image
            run_container
            ;;
        compose)
            run_compose
            ;;
        stop)
            stop_container
            ;;
        clean)
            stop_container
            clean_image
            ;;
        logs)
            view_logs
            ;;
        help|--help|-h)
            show_help
            ;;
        *)
            print_error "无效选项: $1"
            show_help
            exit 1
            ;;
    esac
}

# 执行主函数
main "$@" 